package cl.tinet.finalexam;

import java.io.Serializable;
import java.util.Collection;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import cl.tinet.finalexam.ejb.user.UserBeanInterface;
import cl.tinet.finalexam.exceptions.UserBadCredentialsException;
import cl.tinet.finalexam.exceptions.UserNotFoundException;
import cl.tinet.finalexam.model.UserModel;

@ManagedBean
@SessionScoped
public class UserMB implements Serializable {

    /**
     * TODO doc information expected for serialVersionUID attribute.
     */
    private static final long serialVersionUID = 1L;

    @EJB(mappedName = "userbeanImpl", name = "userbeanImpl")
    private transient UserBeanInterface userBean;

    private Collection<UserModel> colleUsers;
    
    private UserModel userSession;
    private UserModel userRemove;
   

    public UserMB(){
        
    }
    
    @PostConstruct
    public void init() {
        if (userSession == null) {
            userSession = new UserModel();
        }
        getAllUsers();
    }
    

    public String checkLogin() {

        String goTo = "Error";
        try {
            userSession =
                    userBean.checkLogin(userSession.getUsername(),
                            userSession.getPassword());
            if (userSession.getType() == 0) {
                goTo = "admin";
            } else if (userSession.getType() == 1) {
                goTo = "user";
            } 
        } catch (UserNotFoundException e) {
            System.out.println("ERROR - User not found: " + e.getErr());
            return goTo;
        } catch (UserBadCredentialsException e) {
            System.out.println("ERROR - Wrong User or Pass: " + e.getErr());
            return goTo;
        }

        return goTo;
    }


    public void getAllUsers(){
       colleUsers = userBean.allUsers();
    }
    
    public void getUserRemove(){
        System.out.println(userSession.getUserID());
    }
    
    /**
     * Allows to get the colleUsers attribute value.
     * @return the colleUsers value.
     */
    public Collection<UserModel> getColleUsers() {
        return colleUsers;
    }


    /**
     * Allows to set the colleUsers attribute value.
     *
     * @param colleUsers new value for colleUsers attribute.
     */
    public void setColleUsers(Collection<UserModel> colleUsers) {
        this.colleUsers = colleUsers;
    }


    /**
     * Allows to get the userBean attribute value.
     * @return the userBean value.
     */
    public UserBeanInterface getUserBean() {
        return userBean;
    }

    /**
     * Allows to set the userBean attribute value.
     *
     * @param userBean new value for userBean attribute.
     */
    public void setUserBean(UserBeanInterface userBean) {
        this.userBean = userBean;
    }

    /**
     * Allows to get the userSession attribute value.
     * @return the userSession value.
     */
    public UserModel getUserSession() {
        return userSession;
    }

    /**
     * Allows to set the userSession attribute value.
     *
     * @param userSession new value for userSession attribute.
     */
    public void setUserSession(UserModel userSession) {
        this.userSession = userSession;
    }
    
    

}
